package com.lmiot.xyclick.wechat.uikit;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.text.format.DateFormat;

import com.alipay.sdk.packet.e;
import com.lmiot.xyclick.Util.LogUtil;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.security.MessageDigest;

public final class CameraUtil {
    private static final String TAG = "MicroMsg.SDK.CameraUtil";
    private static String filePath;

    private CameraUtil() {
    }

    public static boolean takePhoto(Activity activity, String str, String str2, int i) {
        filePath = str + str2;
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        if (!new File(str).exists()) {
            return false;
        }
        intent.putExtra("output", Uri.fromFile(new File(filePath)));
        try {
            activity.startActivityForResult(intent, i);
            return true;
        } catch (ActivityNotFoundException unused) {
            return false;
        }
    }

    public static String getResultPhotoPath(Context context, Intent intent, String str) {
        if (filePath == null || !new File(filePath).exists()) {
            return resolvePhotoFromIntent(context, intent, str);
        }
        return filePath;
    }

    public static String resolvePhotoFromIntent(Context context, Intent intent, String str) {
        String str2 = null;
        if (!(context == null || intent == null || str == null)) {
            Cursor query = context.getContentResolver().query(Uri.parse(intent.toURI()), null, null, null, null);
            if (query != null && query.getCount() > 0) {
                try {
                    query.moveToFirst();
                    str2 = query.getString(query.getColumnIndex("_data"));
                    LogUtil.d(TAG, "photo from resolver, path:" + str2);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (intent.getData() != null) {
                String path = intent.getData().getPath();
                if (new File(path).exists()) {
                    str2 = path;
                }
                LogUtil.d(TAG, "photo file from data, path:" + str2);
            } else if (intent.getAction() != null && intent.getAction().equals("inline-data")) {
                try {
                    str2 = str + (getMessageDigest(DateFormat.format("yyyy-MM-dd-HH-mm-ss", System.currentTimeMillis()).toString().getBytes()) + ".jpg");
                    Bitmap bitmap = (Bitmap) intent.getExtras().get(e.k);
                    File file = new File(str2);
                    if (!file.exists()) {
                        file.createNewFile();
                    }
                    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bufferedOutputStream);
                    bufferedOutputStream.close();
                    LogUtil.d(TAG, "photo image from data, path:" + str2);
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            } else if (query != null) {
                query.close();
            }
            if (query != null) {
                query.close();
            }
            return str2;
        }
        return null;
    }

    private static final String getMessageDigest(byte[] bArr) {
        char[] cArr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            MessageDigest instance = MessageDigest.getInstance("MD5");
            instance.update(bArr);
            byte[] digest = instance.digest();
            int length = digest.length;
            char[] cArr2 = new char[(length * 2)];
            int i = 0;
            for (byte b : digest) {
                int i2 = i + 1;
                cArr2[i] = cArr[(b >>> 4) & 15];
                i = i2 + 1;
                cArr2[i2] = cArr[b & 15];
            }
            return new String(cArr2);
        } catch (Exception unused) {
            return null;
        }
    }
}
